//! # 依赖注入核心接口定义
//! 
//! 本模块定义了依赖注入容器的核心接口，包括容器接口、组件生命周期管理接口和组件工厂接口。

use rustcloud_core::{ServiceResult, ServiceError};
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::sync::Arc;
use async_trait::async_trait;

/// 依赖注入容器核心接口
#[async_trait]
pub trait Container: Send + Sync {
    /// 注册组件到容器中
    async fn register<T: 'static>(
        &self,
        metadata: ComponentMetadata,
        factory: impl ComponentFactory<T> + 'static,
    ) -> ServiceResult<()>;

    /// 从容器中解析组件实例
    async fn resolve<T: 'static>(&self) -> ServiceResult<Arc<T>>;

    /// 检查组件是否已注册
    async fn is_registered<T: 'static>(&self) -> bool;

    /// 启动容器及所有组件
    async fn start(&self) -> ServiceResult<()>;

    /// 停止容器及所有组件
    async fn stop(&self) -> ServiceResult<()>;
}

/// 组件生命周期管理接口
#[async_trait]
pub trait ComponentLifecycleManager: Send + Sync {
    /// 初始化组件
    async fn initialize(&self) -> ServiceResult<()> { Ok(()) }
    
    /// 启动组件
    async fn start(&self) -> ServiceResult<()> { Ok(()) }
    
    /// 停止组件
    async fn stop(&self) -> ServiceResult<()> { Ok(()) }
    
    /// 销毁组件
    async fn destroy(&self) -> ServiceResult<()> { Ok(()) }
    
    /// 获取组件当前状态
    async fn get_state(&self) -> ComponentState { ComponentState::Running }
    
    /// 执行组件健康检查
    async fn health_check(&self) -> ServiceResult<bool> { Ok(true) }
}

/// 组件工厂接口
#[async_trait]
pub trait ComponentFactory<T>: Send + Sync {
    /// 创建组件实例
    async fn create(&self, container: &dyn Container) -> ServiceResult<Arc<T>>;
    
    /// 获取组件依赖列表
    fn dependencies(&self) -> Vec<TypeId> { Vec::new() }
    
    /// 获取工厂名称
    fn name(&self) -> &str { std::any::type_name::<T>() }
}